<%@ tag language="java" pageEncoding="UTF-8" body-content="empty" trimDirectiveWhitespaces="true"%>
<%@ attribute name="id" type="java.lang.String" required='true' description="ajax请求的id"%>
<%@ attribute name="formSubmit" type="java.lang.Boolean" description="是否需要操作一个表单，true/false，默认为false，设置了这个属性还需要设置formQuery以此告知哪个表单"%>
<%@ attribute name="formQuery" type="java.lang.String" description="jquery的form查询，指明你需要用哪个form，例：若form表单的id='queryOrgFrm'，那么你可以设置formQuery='#queryOrgFrm'"%>
<%@ attribute name="url" type="java.lang.String" %>
<%@ attribute name="type" type="java.lang.String" description="post/get/delete...,默认为get"%>
<%@ attribute name="dataType" type="java.lang.String" description="ajax的dataType,默认为json"%>
<%@ attribute name="async" type="java.lang.Boolean" description="同步/异步，true/false"%>
<%@ attribute name="timeout" type="java.lang.Integer" description="ajax的超时时间，单位：毫秒"%>
<%@ attribute name="cache" type="java.lang.Boolean" description="是否使用缓存，true/false"%>
<%@ attribute name="onSuccess" type="java.lang.String" description="响应成功时调用的函数，onSuccess(data,status,xhr)"%>
<%@ attribute name="onError" type="java.lang.String" description="响应失败时调用的函数，onError(xhr,textStatus,errorThrown)"%>
<%@ attribute name="onServiceError" type="java.lang.String" description="业务错误时的回调函数，onServiceError(data,status,xhr)"%>
<%@ attribute name="autoSend" type="java.lang.Boolean" description="设置是否自动发送ajax请求，默认不自动发送，defualut=false"%>
<%@ attribute name="silent" type="java.lang.Boolean" description="发送Ajax请求时，是否设为加载中状态，默认silent=false"%>
<%@ attribute name="onBeforeSend" type="java.lang.String" description="发送请求前执行函数"%>
<%@ attribute name="onComplete" type="java.lang.String" description="请求完成执行函数"%>
<%@ attribute name="contentType" type="java.lang.String" description="对contentType进行设置，默认'application/json'"%>
<script type="text/javascript">
(function(){
	Logger.info("正在初始化ajax:${id}");
	var type="${type}"==""?undefined:"${type}";
	var data="${data}"==""?undefined:"${data}";
	var dataType="${dataType}"==""?undefined:"${dataType}";
	var async="${async}"==""?undefined:"${async}";
	var timeout="${timeout}"==""?undefined:"${timeout}";
	var cache="${cache}"==""?undefined:"${cache}";
	var url = "${url}"==""?undefined:"${url}";
	var formQuery = "${formQuery}";
	var formSubmit="${formSubmit}"==""?false:"${formSubmit}"=="true"; // 是否利用form表单提交，默认false
	var silent = "${silent}"==""?false:"${silent}"=="true"; // 是否利用form表单提交，默认false
	var autoSend = "${autoSend}"==""?false:"${autoSend}"=="true";
	// Logger.info("ajax.tag =>ajax.id = ${id}, autoSend = " + autoSend);
	var contentType = "${contentType}"==""?"application/json":"${contentType}";
	var ajax;
	if(formSubmit){
		ajax = window["${id}"] = new AjaxForm(formQuery, url, type, data, dataType, 
				async, timeout, cache,silent,autoSend);
		ajax.prepare();
	} else {
	 	ajax = window["${id}"] = new CommonAjax(url, type, data, dataType, async,
			timeout, cache,silent,autoSend);
	}
	
	// 绑定事件
	var succFunc = "${onSuccess}" == "" ?undefined : eval("${onSuccess}");
	ajax.bindSuccessEvent(succFunc);
	
	var errFunc = eval("${onError}");
	ajax.bindErrorEvent(errFunc);
	
	//zh
	var beforeSendFunc = "${onBeforeSend}" == "" ?undefined :eval("${onBeforeSend}");
	ajax.bindBeforeSendEvent(beforeSendFunc);
	var onCompleteFunc = "${onComplete}" == "" ?undefined :eval("${onComplete}");
	ajax.bindCompleteSendEvent(onCompleteFunc);
	var onServiceError = "${onServiceError}" == "" ?undefined :eval("${onServiceError}");
	ajax.bindServiceError(onServiceError);
	ajax.setContentType(contentType);
	
	if(ajax.autoSend)
		ajax.send();
	Logger.info("ajax:${id}初始化完毕");
})();
</script>